<?php
/**
 * Created by PhpStorm.
 * User: zgh
 * Date: 2021-08-04
 * Time: 14:40
 */

namespace app\admin\service;


use app\admin\item\ConfigLoader;
use app\admin\pipeline\ItemAdapter;
use app\admin\validate\ConfigValidate;
use app\common\model\ConfigModel;



class ConfigService
{

    public static function getList($params)
    {
        $pagination = filter_pagination(@$params['page'], @$params['limit']);
        $data = [];
        $loader = new ConfigLoader();
        $where = $loader->addVal($params)
            ->addWhere(['status', '<>', -1])
            ->addWhereSearch("name", "str", true)
            ->addWhereSearch("remark", "str", true)
            ->loadWhere();
        $query = ConfigModel::where($where)->order(["id" => "asc"]);
        $data['total'] = $query->count();
        $data['dataList'] = [];
        if ($data['total']) {
            $data['dataList'] = $query->limit($pagination['start'], $pagination['limit'])->select()->toArray();
        }
        return $data;
    }

    public static function save($params)
    {

        $loader = new ConfigLoader();
        $data = $loader->addVal($params)->addVerify(ConfigValidate::class)->loadItem();
        $adapter = new ItemAdapter(ConfigModel::class);
        $res = $adapter->hookAdd("config_hook", [])->save($data);
        if ($res) {
            return $adapter->getId();
        } else {
            return false;
        }
    }


    public static function del($params)
    {
        $adapter = new ItemAdapter(ConfigModel::class);
        return $adapter->hookAdd("config_hook", [])->whereUpdate([["id", "in", $params['id']]], ["status" => -1]);
    }

    public static function getById($id)
    {
        $adapter = new ItemAdapter(ConfigModel::class);
        return $adapter->get($id)->getModel();
    }


}